package com.liunian.subject.infra.basic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liunian.subject.infra.basic.entity.SubjectLabel;
import com.liunian.subject.infra.basic.entity.SubjectMapping;
import com.liunian.subject.infra.basic.mapper.SubjectLabelMapper;
import com.liunian.subject.infra.basic.service.SubjectMappingService;
import com.liunian.subject.infra.basic.mapper.SubjectMappingMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
*
*/
@Service
public class SubjectMappingServiceImpl extends ServiceImpl<SubjectMappingMapper, SubjectMapping> implements SubjectMappingService{

    @Resource
    private SubjectMappingMapper subjectMappingMapper;

    @Override
    public List<SubjectMapping> queryLabelId(SubjectMapping subjectMapping) {
        QueryWrapper<SubjectMapping> wrapper = new QueryWrapper<>();
        wrapper.select("DISTINCT label_id")
                .eq("category_id", subjectMapping.getCategoryId())
                .eq("is_deleted", subjectMapping.getIsDeleted());
        List<SubjectMapping> list = this.list(wrapper);
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        return list;
    }

    @Override
    public List<SubjectMapping> selectLabelIds(Long subjectId) {
        LambdaQueryWrapper<SubjectMapping> wrapper = new LambdaQueryWrapper<>();
        return subjectMappingMapper.selectList(wrapper.select(SubjectMapping::getLabelId).eq(SubjectMapping::getSubjectId, subjectId));
    }

    @Override
    public boolean saveBatchSubjectMapping(List<SubjectMapping> mappingList) {
        return this.saveBatch(mappingList);
    }
}
